//
//  ShowScores.cpp
//  QuizMaster
//
//  Created by Jeppe Nielsen on 3/23/15.
//  Copyright (c) 2015 Jeppe Nielsen. All rights reserved.
//

#include "ShowScores.hpp"
#include "GameWorld.hpp"
#include "MathHelper.hpp"
#include <algorithm>
#include <sstream>

using namespace Nano;

void ShowScores::Initialize() {
    
    Gui* gui = Parent().gui;
    
    pivot = gui->CreatePivot();
    
    Vector2 backgroundSize(15,8);
    
    GameObject* background = gui->CreateControl(pivot, "Background", -backgroundSize*0.5f, backgroundSize);
    background->GetComponent<Colorable>()->Color = Colour::Black(0.5f);

    for (int i=0; i<4; i++) {
        playerFrame[i]=CreatePlayerFrame(&playerName[i], &playerScore[i]);
    }
    
    exitButton = gui->CreateLabelControl(0, "Background", 0,Vector2(1,1), Parent().font, "X", 0.5f);
    exitButton->GetComponent<Touchable>()->Click += event_handler(this, &ShowScores::ExitButtonClicked);
    exitButton->GetComponent<Material>()->Mask = 1;
    exitButton->Children()[0]->GetComponent<Material>()->Mask = 1;
    Disabled();
}

void ShowScores::Update(float dt) {

}

void ShowScores::Render() {

}

void ShowScores::Enabled() {

    std::vector<GamePlayer*> activePlayers;
    
    for (int i=0; i<4; i++) {
        if (!Parent().data.Players[i].IsPlaying) continue;
        activePlayers.push_back(&Parent().data.Players[i]);
    }
    
    
    std::sort(activePlayers.begin(), activePlayers.end(),
    [](const GamePlayer* a, const GamePlayer* b) -> bool
    {
        return a->GetScore() < b->GetScore();
    });
    
    int bestScore = activePlayers[0]->GetScore();
    
    Vector2 size(6.0f,0.5f);
    Vector2 screenSize(10.0f, 6.0f);
    
    float dY = screenSize.y / activePlayers.size();

    Vector2 position = Vector2(0, -screenSize.y * 0.5f + dY * 0.5f);
    Vector2 delta = Vector2(0, dY);

    for (int i=0; i<4; i++) {
        bool isPlayerVisible = i<activePlayers.size();
        Vector2 pos = isPlayerVisible ? position : Vector2(0,-10);
        
        if (isPlayerVisible) {
            playerName[i]->GetComponent<Label>()->Text = activePlayers[i]->Name;
            playerFrame[i]->GetComponent<Colorable>()->Color = activePlayers[i]->color;
            std::stringstream s;
            s<<activePlayers[i]->GetScore();
            playerScore[i]->GetComponent<Label>()->Text = s.str();
            
            if (Parent().data.IsGameFinished) {
                if (activePlayers[i]->GetScore() < bestScore) {
                    pos += Vector2(0,-1);
                }
            }
        }
        playerFrame[i]->GetComponent<Transform>()->Position = pos;
        position += delta;
    }
    
    exitButton->GetComponent<Transform>()->Position = Vector2(-5,-3);
    
    pivot->ToFront();
    pivot->GetComponent<Transform>()->Position = Vector3(0,0,0);
    
    exitButton->ToFront();
}

void ShowScores::Disabled() {
   pivot->GetComponent<Transform>()->Position = Vector3(-20,0,0);
    exitButton->GetComponent<Transform>()->Position = Vector2(0,-10);
}

GameObject* ShowScores::CreatePlayerFrame(GameObject** label, GameObject** score) {

    Vector2 size(6.0f,0.5f);

    Gui* gui = Parent().gui;

    GameObject* player = gui->CreateControl(pivot, "Background", Vector3(0,-3,0), size);
    player->GetComponent<Transform>()->Anchor = size*0.5f;
    player->GetComponent<Transform>()->Position = Vector3(0,-10,0);
    
    GameObject* playerName = gui->CreateLabel(player, 0, size, Parent().font, "Jeppe", 0.5f);
    playerName->GetComponent<Label>()->HAlignment = Font::Left;
    playerName->GetComponent<Label>()->VAlignment = Font::Middle;
    
    *label = playerName;
    
    GameObject* playerScore = gui->CreateLabel(player, 0, size, Parent().font, "Jeppe", 0.5f);
    playerScore->GetComponent<Label>()->HAlignment = Font::Right;
    playerScore->GetComponent<Label>()->VAlignment = Font::Middle;
    
    *score = playerScore;
    
    return player;
}

void ShowScores::ExitButtonClicked(Nano::TouchData d) {
    Parent().CurrentState = Parent().data.IsGameFinished ? "SetupGame" : "SelectQuestion";
}

